package org.dromara.commission.controller;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.commission.domain.bo.LzshCommissionListBo;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.system.domain.vo.SysOssVo;
import org.dromara.system.service.ISysOssService;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.commission.domain.vo.LzshDocumentUploadVo;
import org.dromara.commission.domain.bo.LzshDocumentUploadBo;
import org.dromara.commission.service.ILzshDocumentUploadService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 上传与下载附件
 *
 * @author Lion Li
 * @date 2024-08-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/commission/documentUpload")
public class LzshDocumentUploadController extends BaseController {

    private final ILzshDocumentUploadService lzshDocumentUploadService;

    private final ISysOssService ossService;


    /**
     * 查询上传与下载附件列表
     */
    @SaCheckPermission("commission:documentUpload:list")
    @GetMapping("/list")
    public TableDataInfo<LzshDocumentUploadVo> list(LzshDocumentUploadBo bo, PageQuery pageQuery) {
        return lzshDocumentUploadService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取上传与下载附件详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("commission:documentUpload:query")
    @GetMapping("/{id}")
    public R<LzshDocumentUploadVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(lzshDocumentUploadService.queryById(id));
    }

    /**
     * 新增上传附件
     */
    @SaCheckPermission("commission:commissionList:add")
    @Log(title = "佣金清单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/{ids}")
    public R<Void> add(@NotNull(message = "文件列表不能为空")
                           @PathVariable Long[] ids ) {
        List<SysOssVo> sysOssVos = ossService.listByIds(List.of(ids));
        for(int i=0;i<sysOssVos.size();i++){
            LzshDocumentUploadBo bo = new LzshDocumentUploadBo();
            bo.setOssId(sysOssVos.get(i).getOssId());
            bo.setDocumentName(sysOssVos.get(i).getOriginalName());
            bo.setDocumentType(sysOssVos.get(i).getFileSuffix());
            bo.setDocumentSize(sysOssVos.get(i).getFileSize());
            bo.setCreateBy(sysOssVos.get(i).getCreateBy());
            bo.setCreateTime(sysOssVos.get(i).getCreateTime());
            bo.setDownloads(0);
            bo.setUrl(sysOssVos.get(i).getUrl());
            lzshDocumentUploadService.insertByBo(bo);
        }
        return R.ok("上传成功！");
    }

    /**
     * 添加下载数量
     *
     * @param id OSS对象ID
     */
    @SaCheckPermission("commission:documentUpload:list")
    @GetMapping("/addDownloads/{id}")
    public void download(@PathVariable Long id) {
        LzshDocumentUploadVo lzshDocumentUploadVo = lzshDocumentUploadService.queryById(id);
        if(lzshDocumentUploadVo != null){
            lzshDocumentUploadService.updateDownloadsById(id);
        }
    }

    /**
     * 修改上传与下载附件
     */
    @SaCheckPermission("commission:documentUpload:edit")
    @Log(title = "上传与下载附件", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody LzshDocumentUploadBo bo) {
        return toAjax(lzshDocumentUploadService.updateByBo(bo));
    }

    /**
     * 删除上传与下载附件
     *
     * @param ids 主键串
     */
    @SaCheckPermission("commission:documentUpload:remove")
    @Log(title = "上传与下载附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(lzshDocumentUploadService.deleteWithValidByIds(List.of(ids), true));
    }
}
